package com.mlink.mapper;


import com.mlink.entity.UserEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

	// 登录
	@Select("SELECT * FROM users  WHERE user_name = #{userName} AND pass_word = #{passWord}")
	@Results({
			@Result(property = "userName",  column = "user_name"),
			@Result(property = "nickName",  column = "nick_name"),
			@Result(property = "passWord", column = "pass_word")
	})
	UserEntity login(@Param("userName")  String userName,@Param("passWord") String passWord );


	//	查询项目所有人的工时
	@Select("SELECT users.id, users.user_name, users.pass_word,users.nick_name,users.org,users.role,SUM(record.working_hour) as workingHour\n" +
			"FROM record \n" +
			"LEFT JOIN users\n" +
			"ON record.user_id=users.id  WHERE record.project_id =#{id}\n" +
			"GROUP BY users.id")
	@Results({
			@Result(property = "userName",  column = "user_name"),
			@Result(property = "nickName",  column = "nick_name"),
			@Result(property = "passWord", column = "pass_word")
	})
	List<UserEntity> getProjecAllUser(int id);



	//判断用户是否存在
	@Select("SELECT * FROM users  WHERE user_name = #{userName} ")
	@Results({
			@Result(property = "userName",  column = "user_name"),
			@Result(property = "nickName",  column = "nick_name"),
			@Result(property = "passWord", column = "pass_word")
	})
	List<UserEntity> isUserEixt(@Param("userName")String userName);



	//增加一个
	@Insert("INSERT INTO users(user_name,pass_word,nick_name,org,role,valid) VALUES(#{userName}, #{passWord}, #{nickName},#{org},#{role},#{valid})")
	@SelectKey(statement = "select last_insert_id()", keyProperty = "id", before = false, resultType = int.class)
	int insert( UserEntity users);




//	获取所有
	@Select("SELECT * FROM users")
	@Results({
		@Result(property = "userName",  column = "user_name"),
		@Result(property = "nickName",  column = "nick_name"),
		@Result(property = "passWord", column = "pass_word")
	})
	List<UserEntity> getAll();


	// 根据用户名获取一个
	@Select("SELECT * FROM users  WHERE id = #{id}")
	@Results({
			@Result(property = "userName",  column = "user_name"),
			@Result(property = "nickName",  column = "nick_name"),
			@Result(property = "passWord", column = "pass_word")
	})
	UserEntity getOne(int id);


	// 更新
	@Update("UPDATE users SET user_name=#{userName},nick_name=#{nickName},pass_word=#{passWord},org=#{org},role=#{role},valid=#{valid} WHERE id =#{id}")
	int update(UserEntity user);

    //删除
	@Delete("DELETE FROM users WHERE id =#{id}")
	int delete(int id);


	//使能用户无效
	@Update("UPDATE users SET valid=#{valid} WHERE id =#{id}")
	int enValid(@Param("valid")int valid,@Param("id")int id);

//	SELECT  u.userName, u.nick_name as nickname, a.name as address
//	FROM users as u
//	LEFT JOIN address  as a
//	ON u.addres=a.id

//	@Select("SELECT  u.userName, a.name as address" +
//			"\tFROM users as u\n" +
//			"\tLEFT JOIN address  as a\n" +
//			"\tON u.addres=a.id")
//	@Results({
//			@Result(property = "userSex",  column = "user_sex", javaType = UserSexEnum.class),
//			@Result(property = "nickName", column = "nick_name")
//	})
//

}